<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Bootstrap Material Admin by Bootstrapious.com</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
    <!-- Fontastic Custom icon font-->
    <link rel="stylesheet" href="css/fontastic.css">
    <!-- Google fonts - Poppins -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="css/custom.css">
    <!-- Favicon-->
    <link rel="shortcut icon" href="img/favicon.ico">
    <!-- Tweaks for older IEs--><!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->
  </head>
  <body>
    <div class="page">
      <div class="page-content d-flex align-items-stretch"> 
        <!-- Side Navbar -->
        <div class="content-inner">
          <!-- Page Header-->
          <header class="page-header">
            <div class="container-fluid">
              <h2 class="no-margin-bottom">Add source</h2>
            </div>
          </header>
          <!-- Breadcrumb-->
          <div class="breadcrumb-holder container-fluid">
            <ul class="breadcrumb">
              <li class="breadcrumb-item"><a href="index.html">Home</a></li>
              <li class="breadcrumb-item active">Add source</li>
            </ul>
          </div>
          <section class="tables">   
            <div class="container-fluid">
              <div id="all_supports" class="row">
              </div>
            </div>
          </section>
        </div>
      </div>
    </div>
    <!-- JavaScript files-->
    <script type="text/javascript">
    function add_source(add_str, type_str, _this)
    {
        var form = $(_this).parent().parent().children("#key_value_div").children('#key_value_form');
        var post_json = form.serializeArray();
        var data = {'type': add_str, 'sourcetype': type_str}
        $.each(post_json, function(){data[this.name] = this.value;});

        $.post("/thunder-trader", 
            JSON.stringify(data),
            function(data){
                var data_obj = JSON.parse(data);
                alert(data_obj["type"]+":"+data_obj["result"]);
                },"text"
        );

    }

    window.onload = function(){ 
        var href = window.location.href;
        var parts = href.split("?");
        if(parts[0] == href)
            return;
        var add_getsupport = parts[1].split("&");
        var add_str = add_getsupport[0];
        var getsupport_str = add_getsupport[1];
        $.post("/thunder-trader", 
            JSON.stringify({"type": getsupport_str}),
            function(data){
                var js = JSON.parse(data);
                var all_supports = "";
                for(var k in js)
                {
                    if (k == 'type')
                        continue;
                    var html = "<div class=\"col-lg-8\"><div class=\"card\"><div class=\"card-close\">"+
                      "<button id=\"add_source\" type=\"button\" class=\"btn btn-primary\" onclick=\"add_source('"+add_str+"','"+k+"', this)\">ADD</button>"+
                    "</div><div class=\"card-header d-flex align-items-center\">"+
                    "<h3 id=\"sname\" class=\"h4\">"+k+"</h3></div><div id=\"key_value_div\" class=\"card-body\"><form id=\"key_value_form\">";
                    var all_keys = js[k].split(';');
                    for(var i in all_keys)
                    {
                        if(all_keys[i]=="")
                            continue;
                        html+="<div class=\"form-group has-success\">"+
                          "<label class=\"control-label\" for=\"inputSuccess1\">"+all_keys[i]+"</label>"+
                          "<input type=\"text\" class=\"form-control\" name=\""+all_keys[i]+"\" aria-describedby=\"helpBlock2\"></div>";

                    }
                    html+="</form></div></div></div>";
                    all_supports += html;

                };
                $("#all_supports").html(all_supports);

                },"text"
        );
    } 
    </script>
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/popper.js/umd/popper.min.js"> </script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="vendor/jquery.cookie/jquery.cookie.js"> </script>
    <script src="vendor/chart.js/Chart.min.js"></script>
    <script src="vendor/jquery-validation/jquery.validate.min.js"></script>
    <!-- Main File-->
    <script src="js/front.js"></script>
  </body>
</html>
